﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Infrastructures;
using Models;

namespace SponsorshipWeb
{
    public partial class Step2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //var user = AuthUtil.GetAuthentication();
                //if (user == null)
                //    Response.Redirect("../login.html");

                //if (user.CompanyID == 0)
                //    Response.Redirect("guide.html");

                ////string id = "20000001"; //Request.QueryString["ID"];
                ////if (string.IsNullOrWhiteSpace(id))
                ////    Response.Redirect("index.html");

                //hfCompanyID.Value = user.CompanyID.ToString();
                //hfUserID.Value = user.UserID.ToString();

                //string mode = Request.QueryString["mode"];
                //if (mode == "edit")
                //{
                //    hfMode.Value = mode;
                //    BindUI(hfCompanyID.Value);
                //}
            }
        }

        private void BindUI(string id)
        {
            //ICompanyService service = ServiceFactory.CreateInstance<ICompanyService>();
            //var company = service.Get<Company>(hfCompanyID.Value);

            //IUserInfoService userService = ServiceFactory.CreateInstance<IUserInfoService>();
            //var user = userService.Get<UserInfo>(company.ContactNO.ToString());

            //IUtilService utilService = ServiceFactory.CreateInstance<IUtilService>();
            //var lst = utilService.ListFileSystem(company.CompanyNO);

            //txtMail.Text = user.Email;
            //txtPhone.Text = user.Phone;
            //txtName.Text = user.NickName;
            //rblSex.SelectedValue = user.Sex == Sex.Male ? "0" : "1";
            //imgFront.Src = SetImg(lst, FileType.IDFront);
            //imgBack.Src = SetImg(lst, FileType.IDOpposite);
        }

        private string SetImg(IList<FileSystem> lst, FileType type)
        {
            var img = lst.FirstOrDefault(x => x.Fun == type);
            if (img != null)
                return img.Path;

            return string.Empty;
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            //string strError = string.Empty;

            //if (string.IsNullOrWhiteSpace(hfMode.Value))
            //{
            //    if (fuIDFront.HasFile == false || String.IsNullOrEmpty(fuIDFront.PostedFile.FileName) ||
            //        fuIDBack.HasFile == false || String.IsNullOrEmpty(fuIDBack.PostedFile.FileName))
            //    {
            //        strError = "ParseExcel:: No file found, please input a file.";
            //        return;
            //    }
            //}

            //ICompanyService service = ServiceFactory.CreateInstance<ICompanyService>();
            //var company = service.Get<Company>(hfCompanyID.Value);

            //IUserInfoService userService = ServiceFactory.CreateInstance<IUserInfoService>();
            //var user = userService.Get<UserInfo>(company.ContactNO.ToString());
            //user.Email = txtMail.Text;
            //user.Phone = txtPhone.Text;
            //user.NickName = txtName.Text;
            //user.Sex = rblSex.SelectedValue == "0" ? Sex.Male : Sex.Female;

            //userService.Update(user, user.UserNO.ToString());

            //company.ContactIDNO = txtID.Text;
            //service.Update(company, user.UserNO.ToString());

            //IList<FileSystem> lst = null;

            //if (hfMode.Value == "edit" && (fuIDBack.HasFile || fuIDFront.HasFile))
            //{
            //    IUtilService utilService = ServiceFactory.CreateInstance<IUtilService>();
            //    lst = utilService.ListFileSystem(Int32.Parse(hfCompanyID.Value));
            //}

            //SaveFile(fuIDFront, FileType.IDFront, lst);
            //SaveFile(fuIDBack, FileType.IDOpposite, lst);

            //if (hfMode.Value == "edit")
            //    Response.Redirect("info.html");
            //else
            //    Response.Redirect("Step3.aspx?ID=" + hfCompanyID.Value);
        }

        private void SaveFile(FileUpload fu, FileType type, IList<FileSystem> lst)
        {
            //if (!fu.HasFile)
            //    return;

            //string fileName = fu.FileName;
            //string fileExt = Path.GetExtension(fileName);
            //string strDir = SystemConfig.Get("UploadPath") + "Company\\" + hfCompanyID.Value + "\\";

            //if (!Directory.Exists(strDir))
            //    Directory.CreateDirectory(strDir);

            //string strSysName = type.ToString() + DateTime.Now.ToString("yyyyMMddHHmmss") + fileExt;
            //fu.SaveAs(strDir + strSysName);

            //FileSystem fs = null;

            //if (lst != null && lst.Count > 0)
            //    fs = lst.FirstOrDefault(x => x.Fun == type);
            //else
            //    fs = new FileSystem();

            //fs.Fun = type;
            //fs.TypeName = APPRole.Company;
            //fs.Belong = Int32.Parse(hfCompanyID.Value);
            //fs.NewName = strSysName;
            //fs.OldName = fileName;
            //fs.Path = SystemConfig.Get("UploadHttpPath") + "Company/" + hfCompanyID.Value + "/" + strSysName;
            //fs.Status = CommonStatus.Active;
            //fs.InstanceID = SystemConfig.Get("InstanceID");
            //fs.CreateUser = Int32.Parse(hfUserID.Value);

            //IUtilService service = FactoryCreator.Create<IUtilService>();

            //if (lst == null || lst.Count == 0)
            //    service.CreateFile(fs);
            //else
            //    service.UpdateFile(fs);
        }
    }
}
